package com.briup.pts.config.web;

import com.briup.pts.web.interceptor.AuthInterceptor;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author wyq
 * @description TODO
 * @data 2025/5/14 13:56
 */
//表明是一个Spring框架的配置类
@Configuration
@RequiredArgsConstructor
public class SpringWebConfig implements WebMvcConfigurer {

    private final AuthInterceptor authInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authInterceptor)
                //配置拦截哪些路径，即那些路径需要被认证
                .addPathPatterns("/**")
                //设置白名单，即无需登录认证的请求名单
                .excludePathPatterns("/login","/source/**",
                        "/file/upload","/profile/**",
                        "/common/download**",
                        "/common/download/resources**",
                        "swagger-ui.html",
                        "/swagger-resources/**",
                        "/webjars/**",
                        "/*/api-docs/**",
                        "/druid/**",
                        "/favicon.ico",
                        "/doc.html");
    }

    // 重写添加跨域映射的处理
    // 在添加了本方法后，SpringMVC组件中就会多出一个响应过滤器，CorsFilter
    // 那么就会在响应返回浏览器之前，执行该过滤器，
    // 同时在响应的头部添加如 access-control-allow-origin等响应头
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 允许本项目下所有的路径请求都支持跨域
        registry.addMapping("/**")
                // 允许来自所有源头的网站的跨域请求
                .allowedOriginPatterns("*")
                .allowedMethods("*")
                .allowedHeaders("*");
    }
}
